package com.css.apps.base.post.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.post.model.SPost;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * <p>
 * 查询岗位
 * </p>
 */
@SuppressWarnings("serial")
public class DirPost extends AdminAction {

	private static Log log = LogFactory.getLog(DirPost.class);
	private String postName;
	private Page page;

	public DirPost() {
		page = new Page();
		page.setCountField("a.uuid");
	}

	@Override
	protected String adminGo() {
		try {
			QueryCache qc = new QueryCache(" select a.uuid from SPost a  " + getWhere() + getOrder());
			setWhere(qc);
			page = qc.page(page);
			page.setResults(QueryCache.idToObj(SPost.class, page.getResults()));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if (StringHelper.isNotEmpty(postName))
			sb.append(" and a.name like :postName ");
		return sb.toString();
	}

	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : "order by a.name";
	}

	public void setWhere(QueryCache qc) {
		if (StringHelper.isNotEmpty(postName))
			qc.setParameter("postName", "%" + postName.trim() + "%");
	}

	public String getPostName() {
		return postName;
	}

	public void setPostName(String postName) {
		this.postName = postName;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
